﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using Foasis.Video;
using Foasis.Video.AviSynth;

namespace AVSRadian
{
	public partial class MainForm : Form
	{
		public MainForm()
		{
			InitializeComponent();
		}

		private void buttonImportSource_Click(object sender, EventArgs e)
		{
			if (openFileDialogSource.ShowDialog() == DialogResult.OK)
			{
				//关闭旗标和计时器，进行初始化；
				viewBoxMain.Start = false;
				//判断输入格式；
				switch (Path.GetExtension(openFileDialogSource.FileName).ToLower())
				{
					case ".avs":
						//如果上一个文件未释放则释放上一个文件；
						//否则接口改变后，close方法无法正确释放资源；
						if (viewBoxMain.VideoAttribute != null)
							viewBoxMain.VideoAttribute.Close();
						viewBoxMain.VideoAttribute = new AviSynth();
						break;
					default:
						MessageBox.Show("Unknow Input.");
						return;
				}
				//读取文件；
				viewBoxMain.VideoAttribute.Open(openFileDialogSource.FileName);
				//重新绘制主窗口控件大小；
				this.MinimumSize = this.MaximumSize = new System.Drawing.Size(viewBoxMain.VideoAttribute.Size.Width + 40, viewBoxMain.VideoAttribute.Size.Height + 145);
				//重新绘制frame控件大小；
				viewBoxMain.Size = viewBoxMain.VideoAttribute.Size;
				//重新绘制浏览输入源按钮；
				buttonImportSource.Location = new System.Drawing.Point(12, viewBoxMain.VideoAttribute.Size.Height + 309 - 240);
				buttonImportSource.Update();
				//重新绘制OCRTime按钮；
				buttonOCRTime.Location = new Point(93, viewBoxMain.VideoAttribute.Size.Height + 309 - 240);
				buttonOCRTime.Update();
				//重新绘制坐标显示框；
				this.groupBoxPos.Location = new Point(viewBoxMain.VideoAttribute.Width - 320 + 237, viewBoxMain.VideoAttribute.Height - 240 + 309);
				//重新绘制trackBarMain；
				trackBarMain.Maximum = viewBoxMain.VideoAttribute.FrameCount;
				trackBarMain.Value = 0;
				trackBarMain.SetBounds(12, viewBoxMain.VideoAttribute.Size.Height + 18, viewBoxMain.VideoAttribute.Size.Width, 45);
				//展开旗标，标识刷新循环和计时器开始；
				viewBoxMain.Start = true;
			}
		}

		private void trackBarMain_ValueChanged(object sender, EventArgs e)
		{
			viewBoxMain.VideoAttribute.CurrentFrameNumber = trackBarMain.Value;
			viewBoxMain.Start = true;
		}

		private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
		{
			//关闭输入文件，释放资源；
			if (viewBoxMain.VideoAttribute != null)
				viewBoxMain.VideoAttribute.Close();
		}

		private void viewBoxMain_MouseMove(object sender, MouseEventArgs e)
		{
			this.x.Text = e.X.ToString();
			this.y.Text = e.Y.ToString();
		}

		private void viewBoxMain_MouseLeave(object sender, EventArgs e)
		{
			this.x.Text = this.y.Text = this.labelComma.Text = null;
		}

		private void viewBoxMain_MouseEnter(object sender, EventArgs e)
		{
			this.labelComma.Text = ",";
		}

		private void buttonOCRTime_Click(object sender, EventArgs e)
		{

		}
	}
}
